package cn.iocoder.yudao.module.train.service.appointment;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.AppointmentDTO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.train.controller.admin.appointment.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.appointment.AppointmentDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.train.dal.mysql.appointment.AppointmentMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.train.enums.ErrorCodeConstants.*;

/**
 * 预约 Service 实现类
 *
 * @author waynelam
 */
@Service
@Validated
public class AppointmentServiceImpl implements AppointmentService {

    @Resource
    private AppointmentMapper appointmentMapper;

    @Override
    public Integer createAppointment(AppointmentSaveReqVO createReqVO) {
        // 插入
        AppointmentDO appointment = BeanUtils.toBean(createReqVO, AppointmentDO.class);
        appointmentMapper.insert(appointment);

        // 返回
        return appointment.getId();
    }

    @Override
    public void updateAppointment(AppointmentSaveReqVO updateReqVO) {
        // 校验存在
        validateAppointmentExists(updateReqVO.getId());
        // 更新
        AppointmentDO updateObj = BeanUtils.toBean(updateReqVO, AppointmentDO.class);
        appointmentMapper.updateById(updateObj);
    }

    @Override
    public void deleteAppointment(Integer id) {
        // 校验存在
        validateAppointmentExists(id);
        // 删除
        appointmentMapper.deleteById(id);
    }

    @Override
        public void deleteAppointmentListByIds(List<Integer> ids) {
        // 删除
        appointmentMapper.deleteByIds(ids);
        }


    private void validateAppointmentExists(Integer id) {
        if (appointmentMapper.selectById(id) == null) {
            throw exception(APPOINTMENT_NOT_EXISTS);
        }
    }

    @Override
    public AppointmentDO getAppointment(Integer id) {
        return appointmentMapper.selectById(id);
    }

    @Override
    public PageResult<AppointmentDTO> getAppointmentPage(AppointmentPageReqVO pageReqVO) {
        return appointmentMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AppointmentDTO> getAppointmentList(AppointmentPageReqVO pageReqVO) {
        return appointmentMapper.selectList(pageReqVO);
    }

}
